package com.hdcx.Minfresh.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.sdk.android.push.noonesdk.PushServiceFactory;
import com.google.gson.Gson;
import com.hdcx.Minfresh.R;
import com.hdcx.Minfresh.bean.UserInfoBean;
import com.hdcx.Minfresh.url.MyUrl;
import com.hdcx.Minfresh.url.RequstCode;
import com.hdcx.Minfresh.util.AppUtil;
import com.hdcx.Minfresh.util.ShpfUtil;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Administrator on 2016/11/18.
 */

public class LoginActivity extends BaseActivity {
    EditText ed_phone,ed_psw;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_login);
        initView();
        initData();
    }

    private void initView() {
        ed_phone = (EditText) findViewById(R.id.ed_phone);
        ed_psw = (EditText) findViewById(R.id.ed_psw);
        findViewById(R.id.bt_upload).setOnClickListener(this);
        findViewById(R.id.tv_left).setOnClickListener(this);
        TextView right = (TextView) findViewById(R.id.tv_right);
        findViewById(R.id.tv_lostpsw).setOnClickListener(this);
        right.setText("注册");
        right.setOnClickListener(this);
    }
    private void initData() {

    }


  /*  mobile	登录手机号码
    password 登录密码   //加密后的传上来  加密方式  md5(密码+‘huudon’)*/

    public void post_login() {
        String phone = ed_phone.getText().toString().trim();
        if(!AppUtil.isPhone(phone)){
            Toast.makeText(this, "请输入正确的手机号码！", Toast.LENGTH_SHORT).show();
            return;
        }
        JSONObject json = new JSONObject();
        try {
            String deviceId = PushServiceFactory.getCloudPushService().getDeviceId();
            Log.e("LoginActivity", "LoginActivity--post_login--"+deviceId);
            json.put("mobile", ed_phone.getText().toString().trim());
            json.put("to_key", deviceId);
            json.put("password",AppUtil.getMD5(ed_psw.getText().toString().trim()+"huudon"));
            Log.i("password", "register: "+AppUtil.getMD5(ed_psw.getText().toString().trim()+"huudon"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        OkHttpUtils.postString()
                .url(MyUrl.LOGIN)
                .content(json.toString())
                .build()
                .execute(new Callback<UserInfoBean>() {
                    @Override
                    public UserInfoBean parseNetworkResponse(Response response) throws IOException {
                        String json = response.body().string();
                        return new Gson().fromJson(json, UserInfoBean.class);
                    }

                    @Override
                    public void onError(Call call, Exception e) {
                        Log.i("onError", "onError: "+e.toString());
                    }

                    @Override
                    public void onResponse(UserInfoBean response) {
                        Toast.makeText(LoginActivity.this,response.getInfo(), Toast.LENGTH_SHORT).show();
                        if (response.getStatus() == 1) {
                            ShpfUtil.setObject(ShpfUtil.LOGIN, response);
                            finish();
                        }
                    }
                });
    }



    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bt_upload:
                post_login();
                break;
            case R.id.tv_left:
                finish();
                break;
            case R.id.tv_right:
                startActivityForResult(new Intent(this,RegisterActivity.class), RequstCode.REGISTER);
                break;
            case R.id.tv_lostpsw:
                startActivity(new Intent(this,ResetPswActivity.class));
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(resultCode==RESULT_OK){
            if(requestCode==RequstCode.REGISTER && data !=null){
                setValus(data.getStringExtra("phone"),data.getStringExtra("psw"));
            }
        }

        super.onActivityResult(requestCode, resultCode, data);
    }

    private void setValus(String phone, String psw) {
        ed_phone.setText(phone);
        ed_psw.setText(psw);
    }
}
